//
//  HLIMKitImageContentConfig.m
//  YueYue
//
//  Created by hulu001 on 2024/1/12.
//

#import "HLIMKitImageContentConfig.h"
#import "HLChatKit.h"

@implementation HLIMKitImageContentConfig


/// 计算气泡尺寸
/// @param cellWidth cell的宽
/// @param message 消息
- (CGSize)contentSize:(CGFloat)cellWidth message:(NIMMessage *)message
{
    HLIMKitMessageSetting *setting = [HLChatKit.shareSDK.config setting:message];
    CGFloat bubbleWidth    = 160;
    CGFloat bubbleLeftToContent  = setting.contentInsets.left;
    CGFloat contentRightToBubble = setting.contentInsets.right;
    CGFloat imgWidth = (bubbleWidth - contentRightToBubble - bubbleLeftToContent);

    /// 气泡最大160
    /// 根据图片宽度计算图片高度
    
    CGFloat attachmentImageMinWidth  = (cellWidth / 4.0);
    CGFloat attachmentImageMinHeight = (cellWidth / 4.0);
    CGFloat attachmemtImageMaxWidth  = imgWidth;
    CGFloat attachmentImageMaxHeight = 160;
    
    NIMImageObject *imageObject = (NIMImageObject*)[message messageObject];
    
    CGSize imageSize;
    if (!CGSizeEqualToSize(imageObject.size, CGSizeZero)) {
        imageSize = imageObject.size;
    }
    else
    {
        UIImage *image = [UIImage imageWithContentsOfFile:imageObject.thumbPath];
        imageSize = image ? image.size : CGSizeZero;
    }
    CGSize contentSize = [HLHelper sizeWithImageOriginSize:imageSize
                                                   minSize:CGSizeMake(attachmentImageMinWidth, attachmentImageMinHeight)
                                                   maxSize:CGSizeMake(attachmemtImageMaxWidth, attachmentImageMaxHeight )];
    return contentSize;
}

/// content view 的名字（一般是类名）
/// @param message 消息
- (NSString *)cellContent:(NIMMessage *)message
{
    return @"HLIMKitMessageImageContentView";
}

/// cell内容距离气泡的内间距
/// @param message 消息
- (UIEdgeInsets)contentViewInsets:(NIMMessage *)message;
{
    return [[HLChatKit.shareSDK config] setting:message].contentInsets;
}

@end
